/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#ifndef LIBS_FSD_H
#define LIBS_FSD_H

#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include "./errorr.h"
#include "./string.h"
#include "./malloc.h"

#ifndef SYS_mmap
#ifdef __x86_64__
    #define SYS_mmap __NR_mmap
#else
    #define SYS_mmap __NR_mmap2
#endif
#endif

#define S_ISREG(m)  (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m)  (((m) & S_IFMT) == S_IFDIR)

#define DO_RDONLY   00
#define DO_WRONLY   01
#define DO_RDWR     02
#define DO_CREAT    0100
#define DO_EXCL     0200
#define DO_TRUNC    01000
#define DO_APPEND   02000
#define DO_NONBLOCK 04000
#define DO_DSYNC    010000
#define DO_RSYNC    040000
#define DO_SYNC     0100000
#define DS_IRUSR    00400
#define DS_IWUSR    00200
#define DS_IXUSR    00100
#define DS_IRGRP    00040
#define DS_IWGRP    00020
#define DS_IXGRP    00010
#define DS_IROTH    00004
#define DS_IWOTH    00002
#define DS_IXOTH    00001
#define DS_IFMT     0170000
#define DS_IFREG    0100000
#define DS_IFDIR    0040000
#define DS_IFCHR    0020000
#define DS_IFBLK    0060000
#define DS_IFIFO    0010000
#define DS_IFLNK    0120000
#define DS_IFSOCK   0140000
#define DS_ISUID    04000
#define DS_ISGID    02000
#define DS_ISVTX    01000
#define DSTDIN_FILENO    0
#define DSTDOUT_FILENO   1
#define DSTDERR_FILENO   2

typedef struct
{
    short 		level;
    unsigned 	flags;
    char 		fd;
    unsigned char 	hold;
    short 		bsize;
    unsigned char 	*buffer;
    unsigned char 	*curp;
    unsigned 	istemp;
    short 		token;
}TFILE;

typedef struct {
    int fd;
    char buf[1024];
    int pos;
} TDIR;

struct Tlinux_dirent {
    uint64_t d_ino;
    uint64_t d_offset;
    unsigned short d_reclen;
    char d_name[];
};

ssize_t     Sread(int fd, void *buf, size_t count);
ssize_t     Sopen(const char *pathname, int flags, mode_t mode);
ssize_t     Swrite(int fd, const void *buf, size_t count);
int 	    Sclose(int fd);
int 	    Smkdir(const char *path, mode_t mode);
int 	    Srmdir(const char *path);
TDIR       *Sopendir(const char *name);
struct      Tlinux_dirent *Sreaddir(TDIR *dirp);
int 	    Sclosedir(TDIR *dirp);
int         Sstat(const char *path, struct stat *buf);
int    	    Sfstat(int fd, struct stat *buf);
const char* Sbasename(const char* path);
char*       Sdirname(const char* path);
int         Sflags(const char *mode);
ssize_t     SXopen(const char *file, const char *mode ,mode_t permissions);
ssize_t     SXread(const char *filename, const char *key, void *buf, size_t bufsize);
int         SXwrite(const char *file, const char *key, const char *value);

#endif //LIBS_FSD_H
